<script setup>
import { ref, onMounted } from 'vue';
import axios from 'axios'
import Edit from './components/Edit.vue'

// TODO: 列表渲染
// 思路:声明响应式list -> 调用接口获取数据 -> 后端数据赋值给list -> 绑定到table组件
const list = ref([])

const getList = async () => {
  // 接口调用(调用mock数据)
  const { data } = await axios.get('/list')
  list.value = data
}


// TODO: 删除功能
const delRow = async (id) => {
  await axios.delete(`/del/${id}`)
  getList()
}

onMounted(() => {
  getList()
})



// TODO: 编辑功能
// 思路:打开弹框 -> 回填数据 -> 更新数据
const editRef=ref()

const form=ref()

// 1.打开弹框 (获取子组件实例 调用方法或者修改属性)
// 2.回填数据 (调用详情接口 / 当前行的静态数据)
const editRow=(row)=>{
  // console.log(row)
  editRef.value.open(row)
}



</script>

<template>
  <div class="app">
    <el-table :data="list">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="姓名" prop="name" width="150"></el-table-column>
      <el-table-column label="籍贯" prop="place"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button type="primary" link @click="editRow(row)" >编辑</el-button>
          <el-button type="danger" link @click="delRow(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <Edit ref="editRef" :form="form" @on-update="getList" />
</template>

<style scoped>
.app {
  width: 980px;
  margin: 100px auto 0;
}
</style>
